package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.MedicConstant;
import com.csun.cmny.provider.model.domain.OmcMedicPlan;
import com.csun.cmny.provider.model.dto.medic.MedicPlanConfirmDto;
import com.csun.cmny.provider.model.dto.medic.MedicPlanDto;
import com.csun.cmny.provider.model.dto.medic.MedicPlanQueryDto;
import com.csun.cmny.provider.model.dto.medic.MedicPlanSignDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcMedicPlanService;
import com.csun.cmny.provider.model.vo.MedicPlanVo;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-13 15:12
 * @Description: the class omc medic plan controller
 */
@RestController
@RequestMapping(value = "/medicPlan", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcMedicPlanController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcMedicPlanService omcMedicPlanService;

    /**
     * 查询用药记录
     *
     * @return
     */
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryListWithPage(@RequestBody MedicPlanQueryDto medicPlanQueryDto) {

        medicPlanQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        List<OmcMedicPlan> omcMedicPlanList = omcMedicPlanService.queryListWithPage(medicPlanQueryDto);

//        return WrapMapper.ok(new PageInfo<>(omcMedicPlanList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), new PageInfo<>(omcMedicPlanList));
    }

    /**
     * 任务查询
     *
     * @return
     */
    @PostMapping(value = "/queryCurTask")
    public Wrapper queryCurTask(@RequestBody MedicPlanQueryDto medicPlanQueryDto) {

        List<Integer> stateList = Lists.newArrayList();
        stateList.add(MedicConstant.MEDIC_PLAN_STATE_APPLY);
        stateList.add(MedicConstant.MEDIC_PLAN_STATE_REFUSE);
        stateList.add(MedicConstant.MEDIC_PLAN_STATE_CONFIRM);

        medicPlanQueryDto.setStates(stateList);
        medicPlanQueryDto.setCreatorId(getLoginAuthDto().getUserId());

        List<OmcMedicPlan> omcMedicPlanList = omcMedicPlanService.queryListWithPage(medicPlanQueryDto);

//        return WrapMapper.ok(new PageInfo<>(omcMedicPlanList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), new PageInfo<>(omcMedicPlanList));
    }

    /**
     * 查询历史记录
     *
     */
    @PostMapping(value = "/queryHisTask")
    public Wrapper queryHisTask(@RequestBody MedicPlanQueryDto medicPlanQueryDto) {

        medicPlanQueryDto.setState(MedicConstant.MEDIC_PLAN_STATE_SIGN);
        medicPlanQueryDto.setCreatorId(getLoginAuthDto().getUserId());
        List<OmcMedicPlan> omcMedicPlanList = omcMedicPlanService.queryListWithPage(medicPlanQueryDto);

//        return WrapMapper.ok(new PageInfo<>(omcMedicPlanList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), new PageInfo<>(omcMedicPlanList));
    }

    /**
     * 用药计划申请
     */
    @ValidateAnnotation
    @PostMapping(value = "/apply")
    public Wrapper apply(@Validated @RequestBody MedicPlanDto medicPlanDto,
                         BindingResult br) {

        Integer result = omcMedicPlanService.apply(medicPlanDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 班长确认
     *
     * @return
     */
    @ValidateAnnotation
    @PostMapping(value = "/confirm")
    public Wrapper confirm(@Validated @RequestBody MedicPlanConfirmDto medicPlanConfirmDto,
                           BindingResult br) {

        Integer result = omcMedicPlanService.monitorConfirm(medicPlanConfirmDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 家属签字
     *
     * @return
     */
    @ValidateAnnotation
    @PostMapping(value = "/sign")
    public Wrapper sign(@Validated @RequestBody MedicPlanSignDto medicPlanSignDto,
                        BindingResult br) {

        Integer result = omcMedicPlanService.familySign(medicPlanSignDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(null);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
    }

    /**
     * 用药计划修改
     *
     * @return
     */
    @PostMapping(value = "/modify")
    public Wrapper modify(@RequestBody MedicPlanDto medicPlanDto) {

        Integer result = omcMedicPlanService.modify(medicPlanDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(null);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
    }
}
